//
//  AppDelegate+Push.m
//  ZLClient
//
//  Created by lizhu on 2017/6/26.
//  Copyright © 2017年 zhuli. All rights reserved.
//

#import "AppDelegate+Push.h"

@implementation AppDelegate (Push)

- (void)registerAPNsWithApplication:(UIApplication *)application{
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType types=UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

        NSLog(@"Requesting permission for push notifications..."); // iOS 8
        UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:types categories:nil];
        [application registerUserNotificationSettings:settings];
    }else {
        UIUserNotificationType types=UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;

        NSLog(@"Registering device for push notifications..."); // iOS 7 and earlier
        [application registerForRemoteNotificationTypes:types];
    }
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings
{
    NSLog(@"Registering device for push notifications..."); // iOS 8
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
{
    NSString *fullDeviceToken = [NSString stringWithFormat:@"%@",token];
    NSString *deviceTokenStr = [fullDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    deviceTokenStr = [deviceTokenStr stringByReplacingOccurrencesOfString:@"<" withString:@""];
    deviceTokenStr = [deviceTokenStr stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSLog(@"Registration successful, bundle identifier: %@, mode: %@, device token: %@",[NSBundle.mainBundle bundleIdentifier], [self modeString], deviceTokenStr);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"Failed to register: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
    NSLog(@"Received push notification: %@", notification); // iOS 7 and earlier
}

- (NSString *)modeString
{
#if DEBUG
    return @"Development (sandbox)";
#else
    return @"Production";
#endif
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)registerLocalPushWithApplication:(UIApplication *)application{
    __block UIBackgroundTaskIdentifier taskID=[application beginBackgroundTaskWithName:@"zhuli" expirationHandler:^{
        [application endBackgroundTask:taskID];
        taskID=UIBackgroundTaskInvalid;
    }];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        while (true) {
            int remainingTime=application.backgroundTimeRemaining;
            if (remainingTime<=5) {
                break;
            }
            NSLog(@"remaining background time :%d",remainingTime);
            
            [NSThread sleepForTimeInterval:1];
        }
        
        [application endBackgroundTask:taskID];
        taskID=UIBackgroundTaskInvalid;
    });
    
    
    UILocalNotification *notification=[[UILocalNotification alloc] init];
    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:3];
    notification.alertTitle=@"alertTitle";
    notification.alertBody=@"alertBody";
    notification.soundName=UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber=10;
    
    [application scheduleLocalNotification:notification];
}
@end
